#include <stdio.h>
#include "Common.h"
#include "WorkerThread.h"

ULONG WINAPI WorkerThreadIOCP(HANDLE hIOCP) {
	DWORD dwRecvNumBytes = 0;
	DWORD dwSendNumBytes = 0;
	DWORD dwFlags = 0;
	OVERLAPPED overlapped;
	TSocketInfo *socketInfo = 0;
	ULONG transferedByte = 0;
	SOCKET connectPayloaderSkt;
	while (true) {
		int bSuccess = GetQueuedCompletionStatus(hIOCP, &transferedByte,
			(ULONG*)&socketInfo, (LPOVERLAPPED*)&overlapped, INFINITE);
		printf("success: %d\n", bSuccess);
		//TODO: maybe need to consider if message not received completely
		char *buf = socketInfo->buf.buf;
		int len = transferedByte;
		buf[len] = 0;
		TOperationInfo opInfo;
		printf("tranByte = %d, buf = '%s'\n", transferedByte, buf);
		sscanf(buf, "%d %d", &opInfo.op, &opInfo.val);
		printf("op = %d, val = %d\n", opInfo.op, opInfo.val);
		switch (opInfo.op) {
		case USER_CONNECT:
			createConnectSocket(PORT2, LOCALHOST, &connectPayloaderSkt);
			break;
		case USER_GETNODE:
			break;
		case USER_RELEASE:
			break;
		case NODE_GETNUM:
			break;
		}
	}
	//closesocket();
	return 0;
}

ULONG WINAPI WorkerThread( void* param )
{
	return 0;
}
